package com.sensePlusVR.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sensePlusVR.utils.MyLog;
import com.umeng.analytics.MobclickAgent;

/**
 * 
 * @author: Loren
 * @create: 2016-8-10下午2:55:43
 * @desc: Fragment 基类
 * 
 * 统计时长只在依附的Activity中处理，所以此处onPause和onResume去除统计时长接口
 * 
 * http://www.jianshu.com/p/850556d33f63（对预加载Fragment特殊处理）
 * 
 */
public class BaseFragment extends Fragment {
	//private static final String TAG_BASE = BaseFragment.class.getSimpleName();
	 //private String TAG_ACTIVITY = TAG +  "-->";
    private final String TAG = getClass().getSimpleName();//当前的子Fragment
    private final String mPageName = TAG;
    //private Context context = getActivity();
	protected boolean isVisible;
	 

	 @Override
	public void onAttach(Context context) {
		super.onAttach(context);
		MyLog.d(TAG, "onAttach");
	}
	 
	 @Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyLog.d(TAG, "onCreate");
	}
	 
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		MyLog.d(TAG, "onCreateView");
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	
	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		MyLog.d(TAG, "onActivityCreated");
	}
	
	
	@Override
	public void onStart() {
		super.onStart();
		MyLog.d(TAG, "onStart");
	}
	
	@Override
	public void onResume() {
		super.onResume();
		MyLog.d(TAG, "onResume");
		MyLog.d(TAG, "mPageName : " + mPageName);
		if(getUserVisibleHint()){
            onVisibilityChangedToUser(true, false);
        }
	}
	
	@Override
	public void onPause() {
		super.onPause();
		MyLog.d(TAG, "onPause");
		  if(getUserVisibleHint()){
	            onVisibilityChangedToUser(false, false);
	        }
	}
	
	@Override
	public void onStop() {
		super.onStop();
		MyLog.d(TAG, "onStop");
	}
	
	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		MyLog.d(TAG, "onDestroyView");
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		MyLog.d(TAG, "onDestroy");
	}
	
	@Override
	public void onDetach() {
		super.onDetach();
		MyLog.d(TAG, "onDetach");
	}
	
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		MyLog.d(TAG, "hidden : " + hidden);
		onVisibilityChangedToUser(!hidden, true);
	}
	
	
	/**
	 * 在这里实现Fragment数据的缓加载.
	 */
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		//isVisibleToUser和getUserVisibleHint()值一样
		MyLog.d(TAG, "setUserVisibleHint isVisibleToUser : " + isVisibleToUser + " isResumed : " + isResumed());
		if (isResumed()) {
			onVisibilityChangedToUser(isVisibleToUser, true);
		}
		if (getUserVisibleHint()) {
			isVisible = true;
			onVisible();
		} else {
			isVisible = false;
			onInvisible();
		}
	}
	
	public void onVisibilityChangedToUser(boolean isVisibleToUser, boolean isHappenedInSetUserVisibleHintMethod){
		//MyLog.d(TAG, "UmengPageTrack isVisibleToUser : " + isVisibleToUser);
        if(isVisibleToUser){
            if(mPageName != null){
            	//MyLog.d(TAG, "UmengPageTrack 111");
                MobclickAgent.onPageStart(mPageName);
                MyLog.d(TAG, "UmengPageTrack : " + mPageName + " - display - ");
            }
        }else{
            if(mPageName != null){
                MobclickAgent.onPageEnd(mPageName);
                MyLog.d(TAG, "UmengPageTrack : " + mPageName + " - hidden - ");
            }
        }
    }

	protected void onVisible(){}

	protected void onInvisible(){}
}
